﻿using AutoDeployForSims.From18To10Cfg.Model;
using AutoDeployForSims.From18To10Cfg.Utility;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace AutoDeployForSims.From18To10Cfg
{
    public class CreateConfig18To10
    {
        //模拟量元素map
        private Dictionary<int, AnalogEnt> analogEntMap = new Dictionary<int, AnalogEnt>();
        //模拟量元素map
        private Dictionary<int, AnalogEnt> analogJYEntMap = new Dictionary<int, AnalogEnt>();
        //模拟量元素map
        private Dictionary<int, AnalogEnt> analogLLEntMap = new Dictionary<int, AnalogEnt>();
        //道岔曲线元素map
        private Dictionary<int, AnalogDCCurve> analogDCCurveEntMap = new Dictionary<int, AnalogDCCurve>();
        //其他曲线元素map
        private Dictionary<int, AnalogOtherCurve> analogOtherCurveEntMap = new Dictionary<int, AnalogOtherCurve>();
        //开关量元素map
        private Dictionary<int, DigitEnt> digitEntMap = new Dictionary<int, DigitEnt>();
        //信号机非正常关闭元素map
        private Dictionary<int, XHJOffAbnormal> xhjOffEntMap = new Dictionary<int, XHJOffAbnormal>();

        /// <summary>
        /// 18版模拟量元素转换为10版元素
        /// </summary>
        /// <param name="analog18EntMap">18版模拟量元素</param>
        /// <param name="analogExtend18EntMap"></param>
        /// <returns></returns>
        public Dictionary<int, AnalogEnt> CreateAnalog10Map(Dictionary<int, AnalogEnt18> analog18EntMap, Dictionary<int, AnalogExtend18> analogExtend18EntMap)
        {
            Debug.WriteLine(analog18EntMap.Count.ToString());
            int changeNum = ChangeEnt18To10(analog18EntMap, analogExtend18EntMap, 1);
            if (changeNum == analog18EntMap.Count)
            {
                Debug.WriteLine("转换成功：" + changeNum);
            }
            return analogEntMap;
        }

        /// <summary>
        /// 18版绝缘模拟量元素转换为10版元素
        /// </summary>
        /// <param name="analog18JYEntMap"></param>
        /// <param name="analogExtend18EntMap"></param>
        /// <returns></returns>
        public Dictionary<int, AnalogEnt> CreateAnalogJY10Map(Dictionary<int, AnalogEnt18> analog18JYEntMap, Dictionary<int, AnalogExtend18> analogExtend18EntMap)
        {
            int changeNum = ChangeEnt18To10(analog18JYEntMap, analogExtend18EntMap, 2);
            if (changeNum == analog18JYEntMap.Count)
            {
                Debug.WriteLine("转换成功：" + changeNum);
            }
            return analogJYEntMap;
        }

        /// <summary>
        /// 18版漏流模拟量元素转换为10版元素
        /// </summary>
        /// <param name="analog18LLEntMap"></param>
        /// <param name="analogExtend18EntMap"></param>
        /// <returns></returns>
        public Dictionary<int, AnalogEnt> CreateAnalogLL10Map(Dictionary<int, AnalogEnt18> analog18LLEntMap, Dictionary<int, AnalogExtend18> analogExtend18EntMap)
        {
            int changeNum = ChangeEnt18To10(analog18LLEntMap, analogExtend18EntMap, 3);
            if (changeNum == analog18LLEntMap.Count)
            {
                Debug.WriteLine("转换成功：" + changeNum);
            }
            return analogLLEntMap;
        }

        private int ChangeEnt18To10(Dictionary<int, AnalogEnt18> analog18EntMap, Dictionary<int, AnalogExtend18> analogExtend18EntMap, int type)
        {
            int changeNum = 0;
            AnalogExtend18 analogExtend18Temp = null;
            //添加普通模拟量转换
            foreach (KeyValuePair<int, AnalogEnt18> kvp in analog18EntMap)
            {
                //创建默认值
                float analogMagnification = 1.0f;
                int magnificationSwitch = 1;
                float Reserve1 = 0.0f;
                float Reserve2 = 0.0f;
                float Reserve3 = 0.0f;
                string Switch1 = "-1";
                string Switch2 = "-1";
                string Switch3 = "-1";
                string Switch4 = "-1";
                string Switch5 = "-1";
                //模拟量报警持续时间，恢复持续时间，波动判断标志，标准值
                string alarmDelayTime = "-1";
                string alarmRecoveryTime = "-1";
                string analogWaveFlag = "-1";
                string analogRefValue = "0";
                //波动幅度，波动频率，分析时长，报警名称
                string analogWaveRange = "-1";
                string analogWaveFrequency = "-1";
                string analogDuration = "-1";
                string alarmName = "-1";
                //模拟量一档突变百分比
                string analogTuBianDuration = "-1";
                //模拟量改方下限
                string analogGFLowerThreshold = "-1";
                string analogGFUpperThreshold = "-1";
                string analogGFFLUpperThreshold = "-1";
                string analogGFDigit = "-1";
                //模拟量前驱查找结点，模拟量后驱查找结点,新增突变百分比2档  和  突变百分比3档
                string preFindTime = "-1";
                string nextFindTime = "-1";
                string TuBianDuration2 = "-1";
                string TuBianDuration3 = "-1";

                int mapKey = kvp.Key;
                AnalogEnt18 mapValue = kvp.Value;
                //标志位
                int analogFlagNum = 0;
                //模拟量类型
                string analog10Type = "";

                //18版漏流标志直流为11，交流为10，18版漏流标志直流为1，交流为0.
                if (type == 3)
                {
                    if (mapValue.AnalogFlag == 11)
                    {
                        analogFlagNum = 1;
                    }
                    else if (mapValue.AnalogFlag == 10)
                    {
                        analogFlagNum = 0;
                    }
                    else
                    {
                        analogFlagNum = mapValue.AnalogFlag;
                    }
                }
                else
                {
                    analogFlagNum = mapValue.AnalogFlag;
                }
                if (type == 3 || type == 2)
                {
                    //绝缘漏流模拟量，10版无类型，为模拟量序号
                    analog10Type = mapValue.AnalogOrder1.ToString();
                }
                else
                {
                    //普通模拟量，18版类型转10版。普通模拟量
                    analog10Type = Change10And18Type.Change18To10(mapValue.AnalogType);
                }
                //通过18版的analog序号找到analogExtend
                if (analogExtend18EntMap.ContainsKey(mapValue.AnalogOrder1))
                {
                    analogExtend18Temp = analogExtend18EntMap[mapValue.AnalogOrder1];
                    Dictionary<string, string> analogProperty = analogExtend18Temp.AnalogProperty;
                    if (analogProperty.ContainsKey("倍率系数"))
                    {
                        analogMagnification = Convert.ToSingle(analogProperty["倍率系数"]);
                    }
                    if (analogProperty.ContainsKey("倍率开关"))
                    {
                        magnificationSwitch = int.Parse(analogProperty["倍率开关"]);
                    }
                    if (analogProperty.ContainsKey("预警上下限"))
                    {
                        //string strReserves = analogEnt10.getReserve1() + ",\t" + analogEnt10.getReserve2() + ",\t" + analogEnt10.getReserve3();
                        string magnificationSwitchTemp = analogProperty["预警上下限"];
                        //规整数据。，去除空格
                        magnificationSwitchTemp = magnificationSwitchTemp.Replace(" ", "");
                        magnificationSwitchTemp = magnificationSwitchTemp.Replace("\t", "");
                        string[] magnificationSwitchStrs = magnificationSwitchTemp.SplitRemoveEndEmptyEntries(new string[] { "," });
                        if (magnificationSwitchStrs.Length >= 3)
                        {
                            Reserve1 = Convert.ToSingle(magnificationSwitchStrs[0]);
                            Reserve2 = Convert.ToSingle(magnificationSwitchStrs[1]);
                            Reserve3 = Convert.ToSingle(magnificationSwitchStrs[2]);
                        }
                    }
                    //统配开关量
                    if (analogProperty.ContainsKey("通配开关量"))
                    {
                        string strSwitch = analogProperty["通配开关量"];
                        //规整数据。，去除空格
                        strSwitch = strSwitch.Replace(" ", "");
                        strSwitch = strSwitch.Replace("\t", "");
                        string[] strSwitchs = strSwitch.SplitRemoveEndEmptyEntries(new string[] { "," });
                        if (strSwitchs.Length >= 4)
                        {
                            Switch1 = strSwitchs[0];
                            Switch2 = strSwitchs[1];
                            Switch3 = strSwitchs[2];
                            Switch4 = strSwitchs[3];
                        }
                    }
                    //模拟量报警持续时间，恢复持续时间，波动判断标志，标准值
                    if (analogProperty.ContainsKey("报警延迟时间"))
                    {
                        alarmDelayTime = analogProperty["报警延迟时间"];
                        //规整数据。，去除空格
                        alarmDelayTime = alarmDelayTime.Replace(" ", "");
                        alarmDelayTime = alarmDelayTime.Replace("\t", "");
                    }
                    if (analogProperty.ContainsKey("报警恢复时间"))
                    {
                        alarmRecoveryTime = analogProperty["报警恢复时间"];
                        //规整数据。，去除空格
                        alarmRecoveryTime = alarmRecoveryTime.Replace(" ", "");
                        alarmRecoveryTime = alarmRecoveryTime.Replace("\t", "");
                    }
                    if (analogProperty.ContainsKey("波动判断标志"))
                    {
                        analogWaveFlag = analogProperty["波动判断标志"];
                        //规整数据。，去除空格
                        analogWaveFlag = analogWaveFlag.Replace(" ", "");
                        analogWaveFlag = analogWaveFlag.Replace("\t", "");
                    }
                    if (analogProperty.ContainsKey("校对调整值"))
                    {
                        analogRefValue = analogProperty["校对调整值"];
                        //规整数据。，去除空格
                        analogRefValue = analogRefValue.Replace(" ", "");
                        analogRefValue = analogRefValue.Replace("\t", "");
                    }
                    //波动幅度，波动频率，分析时长，报警名称
                    if (analogProperty.ContainsKey("波动幅度"))
                    {
                        analogWaveRange = analogProperty["波动幅度"];
                        //规整数据。，去除空格
                        analogWaveRange = analogWaveRange.Replace(" ", "");
                        analogWaveRange = analogWaveRange.Replace("\t", "");
                    }
                    if (analogProperty.ContainsKey("波动频率"))
                    {
                        analogWaveFrequency = analogProperty["波动频率"];
                        //规整数据。，去除空格
                        analogWaveFrequency = analogWaveFrequency.Replace(" ", "");
                        analogWaveFrequency = analogWaveFrequency.Replace("\t", "");
                    }
                    if (analogProperty.ContainsKey("分析时长"))
                    {
                        analogDuration = analogProperty["分析时长"];
                        //规整数据。，去除空格
                        analogDuration = analogDuration.Replace(" ", "");
                        analogDuration = analogDuration.Replace("\t", "");
                    }
                    if (analogProperty.ContainsKey("报警明文设置"))
                    {
                        alarmName = analogProperty["报警明文设置"];
                        //规整数据。，去除空格
                        alarmName = alarmName.Replace(" ", "");
                        alarmName = alarmName.Replace("\t", "");
                    }
                    //模拟量一档突变百分比
                    if (analogProperty.ContainsKey("一档突变百分比"))
                    {
                        analogTuBianDuration = analogProperty["一档突变百分比"];
                        //规整数据。，去除空格
                        analogTuBianDuration = analogTuBianDuration.Replace(" ", "");
                        analogTuBianDuration = analogTuBianDuration.Replace("\t", "");
                    }
                    //模拟量改方下限
                    if (analogProperty.ContainsKey("改方上下限"))
                    {
                        string strGFLTemp = analogProperty["改方上下限"];
                        //规整数据。，去除空格
                        strGFLTemp = strGFLTemp.Replace(" ", "");
                        strGFLTemp = strGFLTemp.Replace("\t", "");
                        string[] strGFLStrs = strGFLTemp.SplitRemoveEndEmptyEntries(new string[] { "," });
                        if (strGFLStrs.Length >= 3)
                        {
                            analogGFLowerThreshold = strGFLStrs[0];
                            analogGFUpperThreshold = strGFLStrs[1];
                            analogGFFLUpperThreshold = strGFLStrs[2];
                        }
                    }
                    if (analogProperty.ContainsKey("改方开关量"))
                    {
                        analogGFDigit = analogProperty["改方开关量"];
                        //规整数据。，去除空格
                        analogGFDigit = analogGFDigit.Replace(" ", "");
                        analogGFDigit = analogGFDigit.Replace("\t", "");
                    }
                    //模拟量前驱查找结点，模拟量后驱查找结点,新增突变百分比2档  和  突变百分比3档
                    if (analogProperty.ContainsKey("稳态前驱查找时间"))
                    {
                        preFindTime = analogProperty["稳态前驱查找时间"];
                        //规整数据。，去除空格
                        preFindTime = preFindTime.Replace(" ", "");
                        preFindTime = preFindTime.Replace("\t", "");
                    }
                    if (analogProperty.ContainsKey("稳态后驱查找时间"))
                    {
                        nextFindTime = analogProperty["稳态后驱查找时间"];
                        //规整数据。，去除空格
                        nextFindTime = nextFindTime.Replace(" ", "");
                        nextFindTime = nextFindTime.Replace("\t", "");
                    }
                    if (analogProperty.ContainsKey("二档突变百分比"))
                    {
                        TuBianDuration2 = analogProperty["二档突变百分比"];
                        //规整数据。，去除空格
                        TuBianDuration2 = TuBianDuration2.Replace(" ", "");
                        TuBianDuration2 = TuBianDuration2.Replace("\t", "");
                    }
                    if (analogProperty.ContainsKey("三档突变百分比"))
                    {
                        TuBianDuration3 = analogProperty["三档突变百分比"];
                        //规整数据。，去除空格
                        TuBianDuration3 = TuBianDuration3.Replace(" ", "");
                        TuBianDuration3 = TuBianDuration3.Replace("\t", "");
                    }

                }
                else
                {
                    Debug.WriteLine("模拟量" + mapValue.AnalogOrder1 + "未找到对应的扩展");
                }

                //创建10版analog元素
                AnalogEnt analogEntTemp = new AnalogEnt(mapValue.Id,
                        mapValue.AnalogName,
                        analogFlagNum,
                        mapValue.AnalogOrder1,
                        analog10Type,
                        mapValue.AnalogRangeLower,
                        mapValue.AnalogRangeUpper,
                        mapValue.AnalogCoefficient,
                        mapValue.AnalogLimitUpper,
                        mapValue.AnalogLimitLower,
                        mapValue.AnalogShuntLimit,
                        mapValue.AnalogExtensionNum,
                        analogMagnification,
                        magnificationSwitch,
                        Reserve1,
                        Reserve2,
                        Reserve3,
                        mapValue.AnalogUnit,
                        Switch1,
                        Switch2,
                        Switch3,
                        Switch4,
                        Switch5,
                        //模拟量报警持续时间，恢复持续时间，波动判断标志，标准值
                        alarmDelayTime,
                        alarmRecoveryTime,
                        analogWaveFlag,
                        analogRefValue,
                        //波动幅度，波动频率，分析时长，报警名称
                        analogWaveRange,
                        analogWaveFrequency,
                        analogDuration,
                        alarmName,
                        //模拟量一档突变百分比
                        analogTuBianDuration,
                        //模拟量改方下限
                        analogGFLowerThreshold,
                        analogGFUpperThreshold,
                        analogGFFLUpperThreshold,
                        analogGFDigit,
                        //模拟量前驱查找结点，模拟量后驱查找结点,新增突变百分比2档  和  突变百分比3档
                        preFindTime,
                        nextFindTime,
                        TuBianDuration2,
                        TuBianDuration3
                );
                if (type == 1)
                {
                    analogEntMap.Add(mapKey, analogEntTemp);
                }
                else if (type == 2)
                {
                    analogJYEntMap.Add(mapKey, analogEntTemp);
                }
                else if (type == 3)
                {
                    analogLLEntMap.Add(mapKey, analogEntTemp);
                }
                changeNum++;
            }
            return changeNum;
        }
    }
}
